package com.yeyks.app.controller.base.order;


import com.github.pagehelper.PageInfo;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.ConfigKey;
import com.yeyks.common.convert.DateEditor;
import com.yeyks.common.utils.Splitters;
import com.yeyks.commonReference.pojo.param.OrderInfoC2bPlaceAnOrderParam;
import com.yeyks.commonReference.pojo.vo.OrderHeadOrderDetailsVo;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.commonReference.utils.UserUtils;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.order.param.*;
import com.yeyks.order.vo.OrderHeadC2bPlaceAnOrderVo;
import com.yeyks.order.vo.OrderHeadFindNoPayOrderVo;
import com.yeyks.order.vo.OrderHeadOrderListVo;
import com.yeyks.order.vo.OrderHeadRefundAmountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author yun
 * @since 2019-07-01
 */
@Api(tags = "订单接口", description = "订单")
@RestController
@RequestMapping("/app/order")
public class OrderHeadController {

    @Autowired
    private OrderHeadService orderHeadService;
    @Autowired
    private ConfigService configService;

    @InitBinder
    public void initbinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(Date.class, new DateEditor());
    }

    @ApiOperation(value = "查询订单:v3.3")
    @RequestMapping(value = "/head/orderList", method = RequestMethod.POST)
    public AjaxResponseBody<PageInfo<OrderHeadOrderListVo>> orderList(@LoadUserId OrderHeadOrderListParam param) {
        PageInfo<OrderHeadOrderListVo> orderHeadPageInfo = orderHeadService.orderList(param);
        return AjaxResponseBody.success(orderHeadPageInfo);
    }

    @ApiOperation(value = "订单详情:v3.3")
    @RequestMapping(value = "/head/orderDetails", method = RequestMethod.POST)
    public AjaxResponseBody<OrderHeadOrderDetailsVo> orderDetails(@Validated @LoadUserId OrderHeadOrderDetailsParam param) {
        OrderHeadOrderDetailsVo orderHeadOrderDetailsVo = orderHeadService.orderDetails(param);
        return AjaxResponseBody.success(orderHeadOrderDetailsVo);
    }

    @ApiOperation(value = "取消订单:v3.6")
    @RequestMapping(value = "/head/cancelOrder", method = RequestMethod.POST)
    public AjaxResponseBody cancelOrder(@Validated @LoadUserId OrderHeadCancelOrderParam param) {
        Boolean b = orderHeadService.cancelOrder(param);
        return AjaxResponseBody.create(b);
    }

    @ApiOperation(value = "删除订单:v3.3")
    @RequestMapping(value = "/head/delOrder", method = RequestMethod.POST)
    public AjaxResponseBody delOrder(@Validated @LoadUserId OrderHeadDelOrderParam param) {
        Boolean b = orderHeadService.delOrder(param);
        return AjaxResponseBody.create(b);
    }

    @ApiOperation(value = "评价订单:v3.3")
    @RequestMapping(value = "/head/evaluateOrder", method = RequestMethod.POST)
    public AjaxResponseBody evaluateOrder(@Validated @LoadUserId OrderHeadEvaluateOrderParam param) {
        Boolean b = orderHeadService.evaluateOrder(param);
        return AjaxResponseBody.create(b);
    }

    //v3.6 下单
    @ApiOperation(value = "下单:v3.6")
    @RequestMapping(value = "/c2b/c2bPlaceAnOrder", method = RequestMethod.POST)
    public AjaxResponseBody<OrderHeadC2bPlaceAnOrderVo> c2bPlaceAnOrder(@Validated @LoadUserId OrderInfoC2bPlaceAnOrderParam param) {
        OrderHeadC2bPlaceAnOrderVo vo = orderHeadService.c2bPlaceAnOrder(param);
        return AjaxResponseBody.success(vo);
    }

    @ApiOperation(value = "获取退款说明和退款原因:v3.3")
    @RequestMapping(value = "/head/refund/desc", method = RequestMethod.POST)
    public AjaxResponseBody<Map<String, List<String>>> refundDesc() {
        String desc = configService.getValue(ConfigKey.REFUND_EXPLAIN);
        String reason = configService.getValue(ConfigKey.REFUND_REASON);
        Map<String, List<String>> map = new HashMap<>();
        map.put("desc", Splitters.splitToString(desc, Splitters.COMMA));
        map.put("reason", Splitters.splitToString(reason, Splitters.COMMA));
        return AjaxResponseBody.success(map);
    }

    @ApiOperation(value = "获取用户应该退款的金额:v3.3")
    @RequestMapping(value = "/head/refund/amount", method = RequestMethod.POST)
    public AjaxResponseBody<OrderHeadRefundAmountVo> refundAmount(@Validated @LoadUserId OrderHeadRefundAmountParam param) {
        OrderHeadRefundAmountVo vo = orderHeadService.refundAmount(param);
        return AjaxResponseBody.success(vo);
    }

    @ApiOperation(value = "查询未完成订单:v4.3.0")
    @RequestMapping(value = "/findNoFinishOrder", method = RequestMethod.POST)
    public AjaxResponseBody<OrderHeadFindNoPayOrderVo> findNoFinishOrder(@Validated @LoadUserId OrderHeadFindNoPayOrderParam param) {
        return AjaxResponseBody.success(orderHeadService.findNoFinishOrder(param));
    }

}
